﻿#region 版本信息
/* ========================================================================
* 【本类功能概述】
* 
* 文件名：DatabaseRegister
* 版本：4.0.30319.42000
* 作者：zl 时间：2016/2/17 15:19:38
* 邮箱：zaixy_8802@126.com
* ========================================================================
*/
#endregion


#region 主体

using Abp.Dependency;
using KelonProud.Data;
using KelonProud.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace KelonProud.Data
{
    public static class DatabaseRegister
    {
        private const string DefaultKey = "Default";
        private const string TypeKey = "DataBase";
        public static void InitDataBase()
        {
            var hostingEnvironment = IocManager.Instance.Resolve<IHostingEnvironment>();
            var configuration = hostingEnvironment.GetAppConfiguration();

            var connStringList = configuration.GetSection("ConnectionStrings");

            foreach (var connString in connStringList.AsEnumerable())
            {
                var paras = connString.Key.Split(':');

                if (paras.Length == 2)
                {
                    if (paras[1] == DefaultKey)
                    {
                        var conn = connString.Value;
                        DatabaseSession.RegisterDefault( new SessionFactory(conn, DatabaseType.MySql));
                    }
                    else
                    {
                        var type = configuration.GetConnectionString(paras[1] + ":Type");
                        var conn = configuration.GetConnectionString(paras[1] + ":Value");
                        var factory = new SessionFactory(conn,DatabaseProviderType.GetDatabaseType(type));

                        DatabaseSession.Register(paras[1], factory);
                    }
                }
            }
        }
    }
}
#endregion
